package array;


import org.junit.Test;

import java.util.Scanner;

//银行叫号   数组模拟队列   先进先出
public class Queue {


    @Test
    public void test() {
        ArrayQueue arrayQueue = new ArrayQueue(3);
        char key = ' '; //接受用户输入
        Scanner sc = new Scanner(System.in);
        boolean flag = true;
        while (flag) {
            System.out.println("s(show) 查看队列所有信息");
            System.out.println("a(add) 往队列添加数据");
            System.out.println("g(get) 往队列取出数据");
            System.out.println("h(head) 查看队列头数据");
            System.out.println("e(exit) 退出! ! !");
            System.out.println("请输入字符:");
            key = sc.next().charAt(0);
            switch (key) {
                case 's':
                    arrayQueue.showQueue();
                    break;
                case 'a':
                    try {
                        System.out.println("请输入一个数字:");
                        int num = sc.nextInt();
                        arrayQueue.addQueue(num);
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'g':
                    try {
                        System.out.println("取出的数据是:" + arrayQueue.getQueue());
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'h':
                    try {
                        System.out.println("队列头数据是:" + arrayQueue.headQueue());
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'e':
                    sc.close();
                    flag = false;
                    break;
                default:
                    break;
            }
        }
        System.out.println("成功退出程序~~~~");
    }

}

class ArrayQueue {
    //定义数组最大值
    private int maxSize;
    private int[] arr;
    private int front; //头指针
    private int rear;  //尾指针

    //构造方法初始化队列
    public ArrayQueue(int maxSize) {
        this.maxSize = maxSize;
        if (maxSize < 1) {
            throw new RuntimeException("初始化队列不能小于1");
        }
        front = -1;
        rear = -1;
        arr = new int[maxSize];
    }

    //判断队列是否为空  如果front ==rear  队列为空
    public boolean isEmpty() {
        return front == rear;
    }

    //判断队列是否满了 rear ==maxSize-1  队列满了
    public boolean isFull() {
        return rear == maxSize - 1;
    }

    //addQueue 添加队列方法
    public void addQueue(int num) {
        //先判断队列是否满了
        if (isFull()) {
            throw new RuntimeException("队列满了~~请稍等");
        }
        //再往队列里添加数据
        rear++; //尾指针往后移动
        arr[rear] = num;
    }

    //showQueue 显示队列所有数据
    public void showQueue() {
        if (isEmpty()) {
            System.out.println("队列为空~~~");
        }
        for (int i = 0; i < arr.length; i++) {
            System.out.printf("a[%d]=%d\t", i, arr[i]);
        }
        System.out.println();
    }

    //getQueue  取出队列中的数据
    public int getQueue() {
        //判断队列是否为空
        if (isEmpty()) {
            throw new RuntimeException("队列为空~~~");
        }
        //一个一个取  从头开始  取完之后 头指针往后移
        front++;
        //返回取出来的数据
        return arr[front];
    }

    //headQueue  查看队列头的数据
    public int headQueue() {
        //判断队列是否为空
        if (isEmpty()) {
            throw new RuntimeException("队列为空~~~");
        }
        return arr[front + 1];
    }


}
